package cn.mumzone.pos.model.source;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.net.source.AbsRepository;
import cn.mumzone.basic.util.RxUtil;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.InWarehouseItemEntity;
import cn.mumzone.pos.model.InventoryDetailEntity;
import cn.mumzone.pos.model.InventoryGoodsEntity;
import cn.mumzone.pos.model.InventoryGoodsResult;
import cn.mumzone.pos.model.InventoryItemEntity;
import cn.mumzone.pos.model.InventoryResultEntity;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.OutWarehouseDetailEntity;
import cn.mumzone.pos.model.OutWarehouseItemEntity;
import cn.mumzone.pos.model.RepoGoodsEntity;
import cn.mumzone.pos.model.RepoListGoodsEntity;
import cn.mumzone.pos.model.RepositoryEntity;
import cn.mumzone.pos.model.WarehouseDetailEntity;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Response;
import retrofit2.http.Field;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by qiuzhenhuan on 2019/7/31.
 */

public class StockRepository extends AbsRepository {

    public void getRepositoryListPage(ApiCallBack<PageListResponse<RepositoryEntity>> callBack) {
        ApiRequest.getStockService()
                .getRepositoryListPage()
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<RepositoryEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void findRepoGoodsByRepoId(String repositoryId, String primaryClassifyId
            , String keyword, String pageNum, String pageSize, String type, ApiCallBack<PageListResponse<RepoListGoodsEntity>> callBack) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("keyWord", keyword);
        jsonObject.put("primaryClassifyId", primaryClassifyId);
        jsonObject.put("repositoryId", repositoryId);
        jsonObject.put("pageNum", pageNum);
        jsonObject.put("pageSize", pageSize);
        jsonObject.put("type", type);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(jsonObject));
        ApiRequest.getStockService()
                .findGoodsByBarCode(requestBody)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<RepoListGoodsEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void findRepoGoodsByRepoIdFromScan(String repositoryId, String barCode, String type, ApiCallBack<PageListResponse<RepoListGoodsEntity>> callBack) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("barCode", barCode);
        jsonObject.put("repositoryId", repositoryId);
        jsonObject.put("type", type);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(jsonObject));
        ApiRequest.getStockService()
                .findGoodsByBarCode(requestBody)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<RepoListGoodsEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getInventoryList(ApiCallBack<PageListResponse<InventoryItemEntity>> callBack, String pageSize, String pageNum
            , String keyword, String startTIme, String endTime, String inventoryType, String inventoryStatus) {
        ApiRequest.getStockService()
                .getInventoryList(pageSize, pageNum, inventoryType, inventoryStatus, startTIme, endTime, keyword)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<InventoryItemEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getInWarehouseList(ApiCallBack<PageListResponse<InWarehouseItemEntity>> callBack, String pageSize, String pageNum
            , String startTime, String endTime, String inCode, String inType, String inStatus) {
        ApiRequest.getStockService()
                .getInWarehouseList(pageSize, pageNum, inType, inStatus, startTime, endTime, inCode)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<InWarehouseItemEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getInWarehouseDetail(ApiCallBack<WarehouseDetailEntity> callBack, String id) {
        ApiRequest.getStockService()
                .getInWarehouseDetail(id)
                .compose(RxUtil.<Response<BaseResponse<WarehouseDetailEntity>>>applySchedulers())
                .subscribe(callBack);

    }


    public void getOutWarehouseList(ApiCallBack<PageListResponse<OutWarehouseItemEntity>> callBack, String pageSize, String pageNum
            , String startTime, String endTime, String inCode, String inType, String inStatus) {
        ApiRequest.getStockService()
                .getOutWarehouseList(pageSize, pageNum, inType, inStatus, startTime, endTime, inCode)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<OutWarehouseItemEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getOutWarehouseDetail(ApiCallBack<OutWarehouseDetailEntity> callBack, String id) {
        ApiRequest.getStockService()
                .getOutWarehouseDetail(id)
                .compose(RxUtil.<Response<BaseResponse<OutWarehouseDetailEntity>>>applySchedulers())
                .subscribe(callBack);

    }

    public void generateCheckStockBill(ApiCallBack<String> callBack, HashMap<String, String> requestMap) {
        ApiRequest.getStockService()
                .addInventory(requestMap)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void generateOutStockBill(ApiCallBack<Object> callBack, HashMap<String, String> requestMap) {
        ApiRequest.getStockService()
                .addStockOut(requestMap)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callBack);
    }

    public void generateInStockBill(ApiCallBack<String> callBack, HashMap<String, String> requestMap) {
        ApiRequest.getStockService()
                .addStockIn(requestMap)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getInventoryDetails(String orderId, ApiCallBack<InventoryDetailEntity> callBack) {
        ApiRequest.getStockService()
                .inventoryDetails(orderId)
                .compose(RxUtil.<Response<BaseResponse<InventoryDetailEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    public void getInventoryGoodsList(String orderId, String keyword, ApiCallBack<InventoryGoodsResult> callBack) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("goodsInventoryId", orderId);
        jsonObject.put("keyWord", keyword);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        ApiRequest.getStockService()
                .inventoryGoodsDetail(body)
                .compose(RxUtil.<Response<BaseResponse<InventoryGoodsResult>>>applySchedulers())
                .subscribe(callBack);
    }


    public void updateInventoryStatus(String orderId, String keyword, ApiCallBack<Object> callBack) {
        ApiRequest.getStockService()
                .updateInventoryStatus(orderId, keyword, "")
                .compose(RxUtil.<Response<BaseResponse<Object>>>applySchedulers())
                .subscribe(callBack);
    }


    public void updateInventoryNum(String inventoryId, List<InventoryResultEntity> goodsList, String inventoryRemark, ApiCallBack<Object> callBack) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("inventoryId", inventoryId);
        jsonObject.put("goodsList", goodsList);
        jsonObject.put("inventoryRemark", inventoryRemark);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        ApiRequest.getStockService()
                .inventoryGoodsNum(body)
                .compose(RxUtil.<Response<BaseResponse<Object>>>applySchedulers())
                .subscribe(callBack);
    }

    public void checkInTheInventory(List<String> goodsIds, ApiCallBack<List<GoodsEntity>> callBack) {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("goodsIds", goodsIds);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        ApiRequest.getStockService()
                .inTheInventory(body)
                .compose(RxUtil.<Response<BaseResponse<List<GoodsEntity>>>>applySchedulers())
                .subscribe(callBack);
    }
}
